<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发布新投票</title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
	<script src="js/axios.min.js"></script>
	<script src="js/vue.min.js"></script>
	<script src="js/vote.js"></script>
	<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
	<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
	<myheader></myheader>
	<div id="voteManage" class="box">
		<h2>添加新投票</h2>
		<div class="content">
		<form method="post" action="view.html">
				<dl>
					<dt>投票内容：</dt>
					<dd>
					   <input type="hidden" v-model="vote.id" value=""/>
					   <input type="text" class="input-text" v-model="vote.vname" value=""/>
					</dd>
					<dt>投票类型：</dt>
					<dd>
					   <input type="radio" v-model="vote.vtype" value="单选" />单选
					   <input type="radio" v-model="vote.vtype" value="多选" />多选
					</dd>
					<dt>投票选项：</dt>

					<dd id="voteoptions">
						<p v-for="item in vote.items">
							<input type="text" v-model="item.iname" class="input-text" readonly />
						</p>
						<p><input type="text" v-model="inameValue" class="input-text" /></p>
					</dd>
					<dt></dt>
					<dd class="button">
						<input type="image" @click.prevent="save" src="images/button_submit.gif" />
						<a href="#" @click.prevent="
							vote.items.push( { iname:inameValue} );
							inameValue=''">增加选项</a>
						<a href="index.html">取消操作</a>
					</dd>
				</dl>
			</form>
		</div>
	</div>
	<myfooter></myfooter>
</div>
<script>
	var v = new Vue({
		el : "#app",
		data : {
			vote : {
				id : "",
				vname : "",
				vtype : "",
				items : [], // {iname:"...", vid:1}
			},
			inameValue : ""
		},
		methods:{
			save(){
				let url = "vote/save.s";
				let params = new URLSearchParams();
				params.append("vname", this.vote.vname);
				params.append("vtype", this.vote.vtype);
				this.vote.items.forEach( item=>{
					// 一组 iname
					params.append("iname", item.iname);
				})
				axios.post(url, params).then(res=>{
					if(res.data.code==1){
						this.$message(res.data.msg);
						location.href="index.html";
					} else {
						this.$alert(res.data.msg);
					}
				})
			}
		}
	})
</script>
</body>
</html>

